package priv.abadstring.study.haoke.webapi.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * RequestReplaceFilter
 *
 * OncePerRequestFilter: 过滤器每次请求只执行一次
 *
 * 通过过滤器进行替换 request 对象
 *
 * @author aBadString
 * @date 2021-07-29 17:54:00
 */
@Component
public class RequestReplaceFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain)
            throws ServletException, IOException {
        if (!(httpServletRequest instanceof MyHttpServletRequestWrapper)) {
            httpServletRequest = new MyHttpServletRequestWrapper(httpServletRequest);
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}
